<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Group as groupModel;
use app\common\model\GroupCategories as groupCateModel;
use app\common\model\Currency as currencyModel;
use Think\Db;

/**
 * 星球管理控制器
 * @package app\admin\controller
 */
class Group extends Admin
{

    /**
     * 星球列表
     * @author jyxxpy
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $map = [];
            $keyword = $this->request->param('keyword');
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            if ($keyword) {
                $map['name'] = ['like', '%' . $keyword . '%'];
            }
            $map['status'] = 1;
            $data['data'] = groupModel::with('hasUser')->with('hasCategories')->where($map)->page($page)->limit($limit)->select();
            $data['count'] = groupModel::where($map)->count('id');
            $data['code'] = 0;
            return json($data);
        }

        return $this->fetch();
    }

    /**
     * 查看星球
     * @author jyxxpy
     * @param  groupId
     * @return mixed
     */
    public function edit($groupId = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Group.update');
            if ($result !== true) {
                return $this->error($result);
            }

            if (!GroupModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }

        $row = GroupModel::with('hasUser')->where('id', $groupId)->find()->toArray();
        $this->assign('data_info', $row);
        $groupCateModel = new groupCateModel();
        $this->assign('category_option',$groupCateModel->getOption());
        return $this->fetch('form');
    }

    /**
     * 星球配置设置
     * @author
     */
    public function set(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $minValue['value'] = $data['min_price'];
            $maxValue['value'] = $data['max_price'];
            Db::name('GroupConfig')->where("`key`='min_price'")->update($minValue);
            Db::name('GroupConfig')->where("`key`='max_price'")->update($maxValue);
        }
        $minPrice = Db::name('GroupConfig')->where("`key`='min_price'")->find();
        $maxPrice = Db::name('GroupConfig')->where("`key`='max_price'")->find();
        $this->assign('minPrice',$minPrice['value']);
        $this->assign('maxPrice',$maxPrice['value']);
        return $this->fetch('set');
    }

    /**
     * token列表
     * @author
     * @return string|\think\response\Json
     * @throws \think\Exception
     */
    public function token(){
        if ($this->request->isAjax()) {
            $map = [];
            $keyword = $this->request->param('keyword');
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            if ($keyword) {
                $map['g.name'] = ['like', '%' . $keyword . '%'];
                $map['c.currency_name'] = ['like', '%' . $keyword . '%'];
            }
            $map['g.status'] = 1;
            $map['c.status'] = 1;
            $groupModel = new groupModel();
            $data['data'] = $groupModel->alias('g')->join('currency c','g.currency_id=c.id')->where($map)->field('g.name,c.*')->page($page)->limit($limit)->select();
            $data['count'] = $groupModel->alias('g')->join('currency c','g.currency_id=c.id')->where($map)->count('c.id');
            $data['code'] = 0;
            return json($data);
        }

        return $this->fetch();
    }

    /**
     * 查看星球
     * @author jyxxpy
     * @param  groupId
     * @return mixed
     */
    public function tokenEdit($currencyId = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!currencyModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }
        $map['g.status'] = 1;
        $map['c.id'] = $currencyId;
        $groupModel = new groupModel();
        $row = $groupModel->alias('g')->join('currency c','g.currency_id=c.id')->where($map)->field('g.name,c.*')->find()->toArray();
        $this->assign('info', $row);
        return $this->fetch('tokeninfo');
    }

}
